<?php

namespace app\service\statistics;

class Order
{
    public function __construct()
    {
        $this->order          = new \app\common\model\Order();
        $this->orderAftersale = new \app\admin\model\order\Aftersale();
    }

    public function statistics()
    {
        #########销售额统计 start #########
        //昨天收入
        $yesterday_money = $this->order
            ->where('pay_time', '>=', time_stamp_start(0, 1, 0))
            ->where('pay_time', '<=', time_stamp_end(0, 1, 0))
            ->cache(true)
            ->sum('remain_pay_fee');
        //今天收入
        $today_money = $this->order
            ->where('pay_time', '>=', time_stamp_start(0, 0, 0))
            ->where('pay_time', '<=', time_stamp_end(0, 0, 0))
            ->sum('remain_pay_fee');
        //近七日收入
        $seven_money = $this->order
            ->where('pay_time', '>=', time_stamp_start(0, 7, 0))
            ->where('pay_time', '<=', time_stamp_end(0, 0, 0))
            ->cache(60)->sum('remain_pay_fee');
        //前七日收入
        $fourteen_money = $this->order
            ->where('pay_time', '>=', time_stamp_start(0, 14, 0))
            ->where('pay_time', '<=', time_stamp_end(0, 7, 0))
            ->cache(true)->sum('remain_pay_fee');

        $yesterday_rate = '0.00';
        if ($yesterday_money) {
            $yesterday_rate = bcdiv(bcsub($today_money, $yesterday_money, 2) * 100, $yesterday_money, 2);
        }
        $seven_rate = '0.00';
        if ($fourteen_money) {
            $seven_rate = bcdiv(bcsub($seven_money, $fourteen_money, 2) * 100, $fourteen_money, 2);
        }
        #########销售额统计 end ############

        ##########订单量统计 start #########
        //昨天订单
        $yesterday_order = $this->order
            ->where('pay_time', '>=', time_stamp_start(0, 1, 0))
            ->where('pay_time', '<=', time_stamp_end(0, 1, 0))
            ->cache(true)->count();
        //今天订单
        $today_order = $this->order
            ->where('pay_time', '>=', time_stamp_start(0, 0, 0))
            ->where('pay_time', '<=', time_stamp_end(0, 0, 0))
            ->cache(60)->count();
        //近七日订单
        $seven_order = $this->order
            ->where('pay_time', '>=', time_stamp_start(0, 7, 0))
            ->where('pay_time', '<=', time_stamp_end(0, 0, 0))
            ->cache(60)->count();
        //前七日订单
        $fourteen_order = $this->order
            ->where('pay_time', '>=', time_stamp_start(0, 14, 0))
            ->where('pay_time', '<=', time_stamp_end(0, 7, 0))
            ->cache(true)->count();

        $yesterday_order_rate = '0.00';
        if ($yesterday_order) {
            $yesterday_order_rate = bcdiv(bcsub($today_order, $yesterday_order, 2) * 100, $yesterday_order, 2);
        }
        $seven_order_rate = '0.00';
        if ($fourteen_order) {
            $seven_order_rate = bcdiv(bcsub($seven_order, $fourteen_order, 2) * 100, $fourteen_order, 2);
        }
        ##########订单量统计 end ###########

        ##########退款件数统计 start #########
        //今日退款件数
        $today_item_refund = $this->orderAftersale
            ->whereDay('create_time')
            ->cache(60)
            ->count();
        //昨天退款件数
        $yesterday_item_refund = $this->orderAftersale
            ->whereDay('create_time', 'yesterday')
            ->cache(true)
            ->count();
        //近七日退款件数
        $seven_item_refund = $this->orderAftersale
            ->where('create_time', '>=', time_stamp_start(0, 7, 0))
            ->where('create_time', '<=', time_stamp_end(0, 0, 0))
            ->cache(60)->count();
        //前七日退款件数
        $fourteen_item_refund = $this->orderAftersale
            ->where('create_time', '>=', time_stamp_start(0, 14, 0))
            ->where('create_time', '<=', time_stamp_end(0, 7, 0))
            ->cache(true)->count();

        $yesterday_item_refund_rate = '0.00';
        if ($yesterday_item_refund > 0) {
            $yesterday_item_refund_rate = bcdiv(bcsub($yesterday_item_refund, $yesterday_item_refund, 2) * 100, $yesterday_item_refund, 2) ?: '0.00';
        }
        $fourteen_item_refund_rate = '0.00';
        if ($fourteen_item_refund > 0) {
            $fourteen_item_refund_rate = bcdiv(bcsub($seven_item_refund, $fourteen_item_refund, 2) * 100, $fourteen_item_refund, 2) ?: '0.00';
        }
        ##########退款件数统计 end ###########

        ##########退款金额统计 start #########
        //今日退款金额
        $today_item_refund_money = $this->orderAftersale
            ->whereDay('create_time')
            ->cache(60)
            ->sum('refund_fee');
        //昨天退款金额
        $yesterday_item_refund_money = $this->orderAftersale
            ->whereDay('create_time', 'yesterday')
            ->cache(true)
            ->sum('refund_fee');
        //近七日退款金额
        $seven_item_refund_money = $this->orderAftersale
            ->where('create_time', '>=', time_stamp_start(0, 7, 0))
            ->where('create_time', '<=', time_stamp_end(0, 0, 0))
            ->cache(60)->sum('refund_fee');
        //前七日退款金额
        $fourteen_item_refund_money = $this->orderAftersale
            ->where('create_time', '>=', time_stamp_start(0, 14, 0))
            ->where('create_time', '<=', time_stamp_end(0, 7, 0))
            ->cache(true)->sum('refund_fee');

        $yesterday_item_refund_money_rate = '0.00';
        if ($yesterday_item_refund_money > 0) {
            $yesterday_item_refund_money_rate = bcdiv(bcsub($today_item_refund_money, $yesterday_item_refund_money, 2) * 100, $yesterday_item_refund_money, 2) ?: '0.00';
        }
        $fourteen_item_refund_money_rate = '0.00';
        if ($fourteen_item_refund_money > 0) {
            $fourteen_item_refund_money_rate = bcdiv(bcsub($seven_item_refund_money, $fourteen_item_refund_money, 2) * 100, $fourteen_item_refund_money, 2) ?: '0.00';
        }
        ##########退款金额统计 end #########
        
        ##########累计订单统计 start #########
        $total_order = $this->order->where('pay_status', 1)->count();
        ##########累计订单统计 end #########
        
        return [
            'total_order'                      => $total_order,
            'today_money'                      => bcmul($today_money, 1, 2),
            'yesterday_rate'                   => $yesterday_rate,
            'seven_rate'                       => $seven_rate,
            'today_order'                      => $today_order,
            'yesterday_order_rate'             => $yesterday_order,
            'seven_order_rate'                 => $seven_order_rate,
            'today_item_refund'                => $today_item_refund,
            'yesterday_item_refund_rate'       => $yesterday_item_refund_rate,
            'fourteen_item_refund_rate'        => $fourteen_item_refund_rate,
            'today_item_refund_money'          => $today_item_refund_money,
            'yesterday_item_refund_money_rate' => $yesterday_item_refund_money_rate,
            'fourteen_item_refund_money_rate'  => $fourteen_item_refund_money_rate,
        ];
    }
}
